﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProgNetComponents.Design.Editors
{
    public class FlagEnumEditor : BaseListEditor
    {
        protected override bool SortItems
        {
            get
            {
                return false;
            }
        }

        protected override bool AlwaysGetValues
        {
            get
            {
                return false;
            }
        }

        private int _CurrentValue = 0;
        private bool _LockEvents = false;

        protected override System.Windows.Forms.ListBox CreateControl()
        {
            CheckedListBox l= new System.Windows.Forms.CheckedListBox();
            l.CheckOnClick = true;
            l.ItemCheck += new ItemCheckEventHandler(ItemCheck);
            return l;
        }

        private void ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (_LockEvents)
                return;

            string key = _ListBox.Items[e.Index].AsString();
            int val = _List[key].AsInt();

            if (val == 0)
                _CurrentValue = 0;
            else if (e.NewValue == CheckState.Checked)
                _CurrentValue |= val;
            else
                _CurrentValue &= (~val);

            SetSelectedItems(_CurrentValue);
        }

        protected override Dictionary<string, object> GetListBoxItems()
        {
            Dictionary<string, object> d = new Dictionary<string, object>();
            if (_Context != null)
            {
                Array arr = Enum.GetValues(_Context.PropertyDescriptor.PropertyType);
                foreach (object o in arr)
                    d.Add(o.ToString(), Convert.ChangeType(o, typeof(int)));
            }
            return d;
        }

        protected override object GetSelectedValue()
        {
            int val = 0;
            string key;
            for(int i =0;i<_ListBox.Items.Count;i++)
            {
                if ((_ListBox as CheckedListBox).GetItemChecked(i))
                {
                    key = _ListBox.Items[i].AsString();
                    if (_List.ContainsKey(key))
                    {
                        val |= _List[key].AsInt();
                    }
                }
            }
            return Enum.ToObject(_Context.PropertyDescriptor.PropertyType, val);
        }

        protected override void listBox_MouseDown(object sender, MouseEventArgs e)
        {
            //base.listBox_MouseDown(sender, e);
        }
        protected override void SetSelectedItems(object value)
        {
            _LockEvents = true;
            int val = (int)(Convert.ChangeType(value, typeof(int)));
            string key ;
            int item;
            _CurrentValue = val;

            for (int i=0; i < _ListBox.Items.Count; i++)
            {
                key = _ListBox.Items[i].AsString();
                if (_List.ContainsKey(key))
                {
                    item = _List[key].AsInt();
                    if (item == 0)
                    {
                        (_ListBox as CheckedListBox).SetItemChecked(i, val == 0);
                    }
                    else if ((val & item) == item && item != 0)
                    {
                        (_ListBox as CheckedListBox).SetItemChecked(i, true);
                    }
                    else
                    {
                        (_ListBox as CheckedListBox).SetItemChecked(i, false);
                    }
                }
            }
            _LockEvents = false;
        }
    }
}

